package com.meixiang.activity.account.manager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.meixiang.R;
import com.meixiang.activity.account.login.LoginPasswordActivity;
import com.meixiang.entity.account.SecurityIndex;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbToastUtil;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 账户安全
 * author：wangzuzhen
 */
public class AccountSafetyActivity extends BaseActivity {
    @Bind(R.id.tv_login_pass_amend)
    TextView tvLoginPassAmend;
    @Bind(R.id.rl_login_pass)
    RelativeLayout rlLoginPass;
    @Bind(R.id.tv_pay_pass_amend)
    TextView tvPayPassAmend;
    @Bind(R.id.rl_pay_pass)
    RelativeLayout rlPayPass;
    @Bind(R.id.tv_bind_phone)
    TextView tvBindPhone;
    @Bind(R.id.rl_bind_phone)
    RelativeLayout rlBindPhone;

    @Bind(R.id.rl_certification)
    RelativeLayout rlCertification;
    @Bind(R.id.tv_realname_type)
    TextView tvRealnameType;
    //    @Bind(R.id.tv_button)
//    TextView tvButton;
    private Activity mActivity;
    private SecurityIndex securityIndex;
    private boolean isAuthentication = false; // 是否已经认证

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_account_safety);
        mActivity= AccountSafetyActivity.this;
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle(getResources().getString(R.string.account_safety));
    }

    @Override
    protected void initView() {

    }


    @Override
    protected void setData() {
    }

    @Override
    protected void onResume() {
        getSecurityTypes();
        super.onResume();
    }


    @OnClick({R.id.rl_login_pass, R.id.rl_pay_pass, R.id.rl_bind_phone, R.id.rl_certification})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_login_pass:
                startActivity(new Intent(this, LoginPasswordActivity.class));
                break;
            case R.id.rl_pay_pass:
                 if (securityIndex.getPaymentPassword().equals("0")){
                     Intent intent=new Intent(mActivity,PayOffSetNewTwoPasswordActivity.class);
                     intent.putExtra("ClassStatus","2s");
                     startActivity(intent);
                 }else {
                     startActivity(new Intent(this, PaymentCodeActivity.class));
                 }
                break;
            case R.id.rl_bind_phone:
                startActivity(new Intent(this, BindPhoneActivity.class));
                break;
            case R.id.rl_certification:
                /**
                 * 已认证传1，未认证传0
                 */
                Intent intent = new Intent(this, VerifyRealNameActivity.class);
                intent.putExtra(VerifyRealNameActivity.IS_AUTHENTICATION, isAuthentication ? 1 : 0);
                startActivity(intent);
                break;
        }
    }


    private void getSecurityTypes() {

        HttpUtils.post(Config.SERCURITYINDEX_URL, null, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                securityIndex =new Gson().fromJson(result.toString(),SecurityIndex.class);


                if (securityIndex.getPaymentPassword().equals("0")) {//"0 未设置  1已设置",
                    tvPayPassAmend.setText("●");
                    tvPayPassAmend.setTextColor(ContextCompat.getColor(mActivity, R.color.magenta));
                }
                if(securityIndex.getPaymentPassword().equals("1")) {
                    tvPayPassAmend.setText("已设置");
                    tvPayPassAmend.setTextColor(ContextCompat.getColor(mActivity, R.color.gray_c7));
                }
                    tvBindPhone.setText(Tool.changeMobile(securityIndex.getMobile()));
                    // "realNameAuthentication": "0 未认证 1认证"
                    if (securityIndex.getRealNameAuthentication().equals("0")) {
                        tvRealnameType.setText("●");
                        tvRealnameType.setTextColor(ContextCompat.getColor(mActivity, R.color.magenta));
                        isAuthentication = false;
                    } else {
                        tvRealnameType.setText("已设置");
                        tvRealnameType.setTextColor(ContextCompat.getColor(mActivity, R.color.gray_c7));
                        isAuthentication = true;
                    }
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(AccountSafetyActivity.this, message);
            }
        });

    }

}
